package libT
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	
	/**
	 * A class designed to make it easier to get tiles from a spritesheet image
	 * @author truefire
	 */
	public class Spritesheet 
	{
		//a BitmapData that holds all the rendered tiles in the sheet once updated. Used as a cache to speed things up
		private var sprite:BitmapData
		
		//temporary point/rect so that a new point or rect does not have to be repeatedly created
		private var tPoint:Point;
		private var tRect:Rectangle;
		
		//variables that define the area of an individual tiles on the sprite
		private var border:int;
		private var edge:int;
		public var tilesW:int;
		public var tilesH:int;
		
		/**
		 * Creates a new Spritesheet
		 * @param	img The spritesheed image to be used
		 * @param	width The width of a single tile
		 * @param	height The height of a single tile
		 * @param	tBorder The spacing between tiles (if any) in pixels
		 * @param	tEdge The size of the border around the grid (if any) in pixels
		 */
		public function Spritesheet(img:Sprite, width:int, height:int, tBorder:int = 0, tEdge:int = 0) 
		{
			sprite = new BitmapData(img.width, img.height);
			
			sprite.draw(img);
			
			tilesW = width;
			tilesH = height;
			
			border = tBorder;
			edge = tEdge;
			
			tPoint = new Point(0, 0);
			tRect = new Rectangle(0, 0, tilesW, tilesH);
		}
		
		/**
		 * Gets a ByteArray corresponding to a tile on the grid
		 * @param x The x coordinate of the tile
		 * @param y The y coordinate of the tile
		 * @return the ByteArray corresponding to the tile
		 */
		public function getTileBA(x:int, y:int):ByteArray
		{
			tRect.x = edge + (border + tilesW) * x;
			tRect.y = edge + (border + tilesH) * y;
			
			return sprite.getPixels(tRect);
		}
		
		/**
		 * Gets an uint Vector corresponding to a tile on the grid
		 * @param x The x coordinate of the tile
		 * @param y The y coordinate of the tile
		 * @return the Vector correspinding to the tile
		 */
		public function getTileVector(x:int, y:int):Vector.<uint>
		{
			tRect.x = edge + (border + tilesW) * x;
			tRect.y = edge + (border + tilesH) * y;
			
			return sprite.getVector(tRect);
		}
		
		/**
		 * Gets a BitmapData corresponding to a tile on the grid
		 * @param x The x coordinate of the tile
		 * @param y The y coordinate of the tile
		 * @return the BitmapData corresponding to the tile
		 */
		public function getTileBMD(x:int, y:int):BitmapData
		{
			var BMD:BitmapData = new BitmapData(tilesW, tilesH);
			
			tPoint.x = x;
			tPoint.y = y;
			BMD.copyPixels(sprite, getTileRect(x, y), tPoint);
			
			return BMD;
		}
		
		/**
		 * Gets the rectangle of the pixels of a tile on the grid
		 * @param x The x coordinate of the tile
		 * @param y The y coordinate of the tile
		 * @return the rectangle corresponding to the tile
		 */
		public function getTileRect(x:int, y:int):Rectangle
		{
			tRect.x = edge + (border + tilesW) * x;
			tRect.y = edge + (border + tilesH) * y;
			
			return tRect.clone();
		}
		
		/**
		 * Draws a tile from the spritesheet to a BitmapData
		 * @param x The x coordinate of the tile
		 * @param y The y coordinate of the tile
		 * @param x2 The x coordinate on the BMD corresponding to the upper left corner of the tile
		 * @param y2 The y coordinate on the BMD corresponding to the upper left corner of the tile
		 * @param BMD The bitmap data to draw to
		 */
		public function drawToBMD(x:int, y:int, x2:int, y2:int, BMD:BitmapData):void
		{
			tPoint.x = x2;
			tPoint.y = y2;
			BMD.copyPixels(sprite, getTileRect(x, y), tPoint);
		}
	}

}